package ink.lovejinhu.swardToOffer;

/**
 * @author jinhu
 * created 2021-08-09 16:44
 */
public class Offer_005 {
    /**
     * 替换空格
     * 把字符串 s 中的每个空格替换成"%20"
     *
     * @param s
     * @return
     */
    public String replaceSpace(String s) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                count++;
            }
        }
        char[] res = new char[s.length() + 2 * count];
        int j = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                res[j] = '%';
                res[j + 1] = '2';
                res[j + 2] = '0';
                j = j + 3;
            } else {
                res[j] = s.charAt(i);
                j++;

            }
        }
        return new String(res);

    }
}
